﻿using MvcApplication2.Bussiness;
using MvcApplication2.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace MvcApplication2.Controllers
{
    public class AuthenticationController : Controller
    {
        //
        // GET: /Authentication/

        public ActionResult Login()
        {
            return View("Login");
        }
        public ActionResult DoLogin(UserDetails u)
        {
            EmployeeBusinessLayer embuss=new EmployeeBusinessLayer();
            if (ModelState.IsValid&&u.UserName=="admin"&&u.PassWord=="123")
            {
                UserStatus us = embuss.GetUserValidity(u);
                bool IsAdmin = true;
                if (us == UserStatus.AuthentucatedUser)
                {
                    IsAdmin = false;
                }
                Session["IsAdmin"] = IsAdmin;
                FormsAuthentication.SetAuthCookie(u.UserName, false);
                return RedirectToAction("Index", "Employee");
            }
            else
            {
                ModelState.AddModelError("CredentialError", "无效的用户名和密码");

                return View("Login");
            }
        }
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
             return RedirectToAction("Login");
        }

    }
}
